﻿using System;
using System.Runtime.InteropServices;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Resources;
using Microsoft.SharePoint;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;

namespace Version3.SharePoint
{
    [Guid("FF8EBF63-5806-42a4-98AD-225CC0384B92")]
    [CLSCompliant(false)]
    public class FreezeDriedDataFieldPropertyEditor : UserControl, IFieldEditor
    {
        protected RadioButton radDisplayNothing;
        protected RadioButton radDisplayDataType;
        protected RadioButton radDisplayFixedText;
        protected RadioButton radDisplayRenderedData;
        protected TextBox txtRenderControlString;
        
        protected RadioButton radColumnDisplayDataType;
        protected RadioButton radColumnDisplayDisplayText;
        protected RadioButton radColumnDisplayDisplayNothing;

        protected Label lblControlString;

        private string _FormRenderControlString = string.Empty;
        private string _FormRenderMode = string.Empty;
        private string _ColumnRenderMode = string.Empty;

        public override void Focus()
        {
            EnsureChildControls();

        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            radDisplayNothing.CheckedChanged += new EventHandler(FormRadiobutton_CheckedChanged);
            radDisplayDataType.CheckedChanged += new EventHandler(FormRadiobutton_CheckedChanged);
            radDisplayFixedText.CheckedChanged += new EventHandler(FormRadiobutton_CheckedChanged);
            radDisplayRenderedData.CheckedChanged += new EventHandler(FormRadiobutton_CheckedChanged);

            if (!Page.IsPostBack)
            {

                radDisplayNothing.Checked = false;
                radDisplayDataType.Checked = false;
                radDisplayFixedText.Checked = false;
                radDisplayRenderedData.Checked = false;
                txtRenderControlString.TextMode = TextBoxMode.SingleLine;
                txtRenderControlString.Rows = 1;
                txtRenderControlString.Text = _FormRenderControlString;

                switch (_FormRenderMode)
                {

                    case FreezeDriedDataField.DATA_TYPE:
                        radDisplayDataType.Checked = true;
                        break;
                    case FreezeDriedDataField.FIXED_TEXT:
                        radDisplayFixedText.Checked = true;
                        break;
                    case FreezeDriedDataField.RENDER_OBJECT:
                        radDisplayRenderedData.Checked = true;
                        txtRenderControlString.TextMode = TextBoxMode.MultiLine;
                        txtRenderControlString.Rows = 4;
                        txtRenderControlString.Wrap = true;
                        break;
                    default:
                        radDisplayNothing.Checked = true;
                        break;
                }

                FormRadiobutton_CheckedChanged(null, null);

                radColumnDisplayDataType.Checked = false;
                radColumnDisplayDisplayText.Checked = false;
                radColumnDisplayDisplayNothing.Checked = false;
                switch (_ColumnRenderMode)
                {
                    case FreezeDriedDataField.DATA_TYPE:
                        radColumnDisplayDataType.Checked = true;
                        break;
                    case FreezeDriedDataField.DISPLAY_TEXT:
                        radColumnDisplayDisplayText.Checked = true;
                        break;
                    default:
                        radColumnDisplayDisplayNothing.Checked = true;
                        break;
                }

            }
       
        }

        void FormRadiobutton_CheckedChanged(object sender, EventArgs e)
        {
            if (radDisplayDataType.Checked || radDisplayNothing.Checked)
            {
                txtRenderControlString.Text = string.Empty;
                txtRenderControlString.Enabled = false;
                txtRenderControlString.ReadOnly = true;
                txtRenderControlString.Rows = 1;
                txtRenderControlString.TextMode = TextBoxMode.SingleLine;
                lblControlString.Text = "&nbsp;";
            }
            else
            {
                txtRenderControlString.Enabled = true;
                txtRenderControlString.ReadOnly = false;
                txtRenderControlString.Visible = true;
                if (radDisplayFixedText.Checked)
                {
                    lblControlString.Text = "Fixed Display text:";
                    txtRenderControlString.TextMode = TextBoxMode.SingleLine;
                    txtRenderControlString.Rows = 1;
                }
                if (radDisplayRenderedData.Checked)
                {
                    lblControlString.Text = "Helper class and assembly path:";
                    txtRenderControlString.TextMode = TextBoxMode.MultiLine;
                    txtRenderControlString.Rows = 4;
                    txtRenderControlString.Wrap = true;
                }
            }
        }


        public bool DisplayAsNewSection
        {
            get
            {
                return true;
            }
        }

        public void InitializeWithField(SPField field)
        {
            if (null != field)
            {
                FreezeDriedDataField sharePointField = (FreezeDriedDataField)field;
                _FormRenderControlString = sharePointField.FormRenderControlString;
                _FormRenderMode = sharePointField.FormRenderMode;
                _ColumnRenderMode = sharePointField.ColumnRenderMode;
            }
        }

        private string FormRenderMode()
        {
            if (radDisplayDataType.Checked) return FreezeDriedDataField.DATA_TYPE;
            if (radDisplayFixedText.Checked) return FreezeDriedDataField.FIXED_TEXT;
            if (radDisplayRenderedData.Checked) return FreezeDriedDataField.RENDER_OBJECT;
            return string.Empty;
        }


        private string ColumnRenderMode()
        {
            if (radColumnDisplayDataType.Checked) return FreezeDriedDataField.DATA_TYPE;
            if (radColumnDisplayDisplayText.Checked) return FreezeDriedDataField.DISPLAY_TEXT;
            return string.Empty;
        }

        public void OnSaveChange(SPField field, bool isNewField)
        {
            FreezeDriedDataField sharePointField = (FreezeDriedDataField)field;
            sharePointField.FormRenderControlString = txtRenderControlString.Text;
            sharePointField.FormRenderMode = FormRenderMode();
            sharePointField.ColumnRenderMode = ColumnRenderMode();
            if (isNewField)
            {
                sharePointField.SetNewColumnPropertyCacheValue(FreezeDriedDataField.FORM_RENDER_CONTROL_STRING_KEY, txtRenderControlString.Text);
                sharePointField.SetNewColumnPropertyCacheValue(FreezeDriedDataField.FORM_RENDER_MODE_KEY, FormRenderMode());
                sharePointField.SetNewColumnPropertyCacheValue(FreezeDriedDataField.COLUMN_RENDER_MODE_KEY, ColumnRenderMode());
            }
        }
    }
}
